<?php
class fvTree{
  var $current_level;
  var $cats_array;
  var $data;
  
  function fvTree($cats_array){
	
	$this->cats_array = $cats_array;

	$this->current_level = 0;
	
	$data = $this->get_tree();
	
	$this->data = $data;
  }
  
  function return_tree(){
	return $this->data;
  }
  
  function remove_node($id){
  	$data = $this->data;
  	
  	$start = 0;
  	for($i = 0; $i < count($data); $i++){
  		if($data[$i]['id'] == $id){
  			$start = 1;
  		    $row = $data[$i];
  		}
  		
  		
  		if($start == 1 & $data[$i]['level'] <= $row['level'] & $data[$i]['id'] != $id)
  		$start = 0;
  		
  		if($start != 1)
  		$ret_array[$i] = $data[$i];
  	}
  	
  	return $ret_array;
  }
  
  
  function extract_fetched_array($first_row, $tree, $count){
  	$data = $tree;
  	
  	$ret_array[0] = $first_row;
  	
  	for($i = 1; $i < $count+1; $i++){
  		unset($row);
  		
  		foreach($data as $key=>$val){
  			if($val['id'] == $i){
  				$row = $val;
  				break;
  			}
  		}
  		
  		if(isset($row)){
  			
  			if($row['parent_id'] == '')
  		    $row['parent_id'] = '0';
  		    
  			$ret_array[$i]['id']     = $row['id'];
  		    $ret_array[$i]['title']   = $row['title'];
  		    $ret_array[$i]['parent_id'] = $row['parent_id'];
  		}
  	}
  	
  	return $ret_array;
  }
  
  function get_tree() {
        $temp_tree = $this->cats_array;
        foreach ($temp_tree as $key => $value) {
            if (!$temp_tree[$key]['parent_id']) {

                $this->current_level = 0;
				
                $new_tree[] = array(
                                    'id'     => $temp_tree[$key]->getPk(),
                                    'title'   => str_repeat('&nbsp;&nbsp;&nbsp;', $this->current_level).$temp_tree[$key]["title"],
                                    'parent_id' => $temp_tree[$key]['parent_id'],
                                    'level'  => $this->current_level
                                   );

                if ($branch = $this->get_leafs($temp_tree, $temp_tree[$key]->getPk())) {

                $new_tree = array_merge($new_tree, $branch);
                }
            }
        }

        return (isset($new_tree) ? $new_tree : false);
    }

    function get_leafs($temp_tree, $id) {

        $this->current_level++;

        foreach ($temp_tree as $key => $value) {
            if ($temp_tree[$key]['parent_id'] == $id) {
                $new_tree[] = array(
                                    'id'     => $temp_tree[$key]->getPk(),
                                    'title'   => str_repeat('&nbsp;&nbsp;&nbsp;', $this->current_level).$temp_tree[$key]["title"],
                                    'parent_id' => $temp_tree[$key]['parent_id'],
                                    'level'  => $this->current_level
                                   );

                if ($branch = $this->get_leafs($temp_tree, $temp_tree[$key]->getPk())) {

                    $new_tree = array_merge($new_tree, $branch);
                }

                $this->current_level--;
            }
        }

        return (isset($new_tree) ? $new_tree : false);
    }
}
?>
